﻿using Ninject;
using Romantics.Domain;
using Romantics.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Romantics.Controllers
{
    public class HikeController : Controller
    {
        private readonly IDomainRepository _domainRepository;

        public HikeController(IDomainRepository domainRepository)
        {
            _domainRepository = domainRepository;
        }

        //
        // GET: /Hike/

        public ViewResult Index(int page = 1)
        {
            // pick latest 10 posts
            var listViewModel = HikeViewModel.Planed(_domainRepository, page);

            ViewBag.Title = "Планируемые походы";

            return View("_List", listViewModel);
        }

        public ViewResult Category(string category_url = "", int page = 1)
        {
            var category = _domainRepository.Category(category_url);
            var listViewModel = HikeViewModel.Planed(_domainRepository, page, category);

            if(category == null)
                ViewBag.Title = "Планируемые походы";
            else
                ViewBag.Title = String.Format("Планируемые походы в категории \"{0}\"",category.Name);
            return View("_List", listViewModel);
        }

        public ViewResult Region(string region_url = "", int page = 1)
        {
            var region = _domainRepository.Region(region_url);
            var listViewModel = HikeViewModel.Planed(_domainRepository, page, null, region);
            if (region == null)
                ViewBag.Title = "Планируемые походы";
            else
                ViewBag.Title = String.Format("Планируемые походы в регионе \"{0}. {1}\"", region.Country.Name, region.Name);
            return View("_List", listViewModel);
        }

        public ViewResult Hike(int id)
        {
            var hike = _domainRepository.Hike(id);

            if (hike == null || !hike.Published)
                throw new HttpException(404, "Post not found");

            hike.Options = _domainRepository.Options();
            return View("_Hike", hike);
        }

        /// <summary>
        /// Child action that returns the sidebar partial view.
        /// </summary>
        /// <returns></returns>
        [ChildActionOnly]
        public PartialViewResult Sidebars(string action)
        {
            var widgetViewModel = new WidgetViewModel(_domainRepository, action);
            if (RouteData.Values.ContainsKey("category_url"))
            {
                ViewBag.Category = _domainRepository.Category(RouteData.Values["category_url"].ToString());
            }
            if (RouteData.Values.ContainsKey("region_url"))
            {
                ViewBag.Region = _domainRepository.Region(RouteData.Values["region_url"].ToString());
            }
            return PartialView("_Sidebars", widgetViewModel);
        }
    }
}
